package com.taptech.doufu.ui.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.bean.personalcenter.PersonalCardInfo;
import com.taptech.doufu.services.personalcenter.PersonalInfoService;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.adapter.personalcenter.PersonalSendLetterAdapter;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.UIUtil;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.ui.view.PullToRefreshListView;

public class PersonalSendLetterActivity extends DiaobaoBaseActivity implements HttpResponseListener
{


    private PullToRefreshListView attentions;
    private PersonalSendLetterAdapter adapter;
    private TextView typeName;
    private View search_ly;
    private EditText search_et;

    private String last = "";
    private boolean hasMoreContent = true;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.personal_center_activity_fans);
        initView();
        PersonalInfoService.getInstance().loadAttentions(last, this);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus)
    {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus)
        {
            if (adapter == null || adapter.getCount() == 0)
            {
                //loadMore();
            }
        }
    }

    private synchronized void loadMore()
    {
        if (hasMoreContent)
        {
            PersonalInfoService.getInstance().loadAttentions(last, this);
            hasMoreContent = false;
        }
    }

    private void initView()
    {
        typeName = (TextView) findViewById(R.id.member_type_name);
        attentions = (PullToRefreshListView) findViewById(R.id.personal_center_fans_list);
        search_ly = (View) findViewById(R.id.search_ly);
        search_et = (EditText) findViewById(R.id.search_activity_article_text);
        adapter = new PersonalSendLetterAdapter(PersonalSendLetterActivity.this);
        typeName.setText("私信");
        initIntent();
        adapter.setDataSource(PersonalInfoService.getInstance().getAttentions());
        attentions.setAdapter(adapter);
        attentions.setLoadmoreable(true);
        attentions.setOnScrollToBottomListener(new PullToRefreshListView.OnScrollToBottomListener()
        {
            @Override
            public void OnScrollToBottom()
            {
                loadMore();
            }
        });
    }

    private void initIntent()
    {
        // TODO Auto-generated method stub
        // intent.putExtra("TYPE", "fromNewUgc");

        if (getIntent().getStringExtra("TYPE") != null)
        {
            String type = getIntent().getStringExtra("TYPE");
            if (type.equals("fromNewUgc"))
            {
                typeName.setText("我的好友");
                search_ly.setVisibility(View.VISIBLE);
                adapter.setType(type);
            }
        }
        search_et.addTextChangedListener(new TextWatcher()
        {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
            {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
            {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0)
            {
                // TODO Auto-generated method stub
                if (search_et.getText().toString().trim().length() > 0)
                {
                    filterContacts(search_et.getText().toString().trim());
                } else
                {
                    adapter.setDataSource(PersonalInfoService.getInstance().getAttentions());
                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    protected void filterContacts(String name)
    {
        // TODO Auto-generated method stub
        List<PersonalCardInfo> cardInfos = PersonalInfoService.getInstance().getAttentions();
        List<PersonalCardInfo> newCardInfos = new ArrayList<PersonalCardInfo>();
        for (int i = 0; i < cardInfos.size(); i++)
        {
            //Log.i("TAG", cardInfos.get(i).getNickname().toString());
            if (cardInfos.get(i).getNickname().toString().contains(name))
            {
                newCardInfos.add(cardInfos.get(i));
            }
        }
        adapter.setDataSource(newCardInfos);
        attentions.setFull(true);
        attentions.loadMoreComplete();
        attentions.setLoadmoreable(false);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void handleResponse(int handleType, HttpResponseObject response)
    {
        try
        {
            if (response.getStatus() == 0)
            {
                JSONArray datas = (JSONArray) response.getData();
                JSONObject meta = new JSONObject(response.getMeta().toString());
                List<PersonalCardInfo> contacts = DiaobaoUtil.jsonArray2BeanList(PersonalCardInfo.class, datas);
                if (last.equals(""))
                {
                    adapter.setDataSource(contacts);
                    hasMoreContent = true;
                } else if (!last.equals(meta.getString("last")))
                {
                    adapter.setSuperDatas(contacts);
                    hasMoreContent = true;
                }

                if (meta != null && !meta.has("last"))
                {
                    attentions.setFull(true);
                    attentions.loadMoreComplete();
                    attentions.setLoadmoreable(false);
                }
                if (meta != null && meta.has("last"))
                {
                    last = meta.getString("last");
                }

            } else
            {
                UIUtil.toastMessage(this, DiaobaoUtil.getErrorTips(response));
            }

        } catch (Exception e)
        {
            e.printStackTrace();
            attentions.setFull(true);
            attentions.loadMoreComplete();
            attentions.setLoadmoreable(false);
        }
    }

    public void back(View view)
    {
        onBackPressed();
    }

}
